package com.ruoyi.project.cms.sa.customer.controller;

import com.ruoyi.common.constant.CMSConstants;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.framework.web.page.TableDataInfo;
import com.ruoyi.project.cms.sa.customer.domain.Customer;
import com.ruoyi.project.cms.sa.customer.service.ICustomerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author ：MrL
 * 时间：2020-05-20
 */
@Controller
@RequestMapping("/cms/sa/customer")
public class CustomerController extends BaseController {

    private String prefix = "cms/sa/customer";

    @Autowired
    private ICustomerService customerService;

    @GetMapping()
    public String customer() {
        return prefix + "/customer";
    }

    /**
     * 根据条件分页查询客户数据
     *
     * @param customer
     * @return
     */
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(Customer customer) {
        startPage();
        List<Customer> list = customerService.selectCustomerList(customer);
        return getDataTable(list);
    }

    /**
     * 新增客户信息
     */
    @GetMapping("/add")
    public String add() {
        return prefix + "/add";
    }

    /**
     * 新增保存客户信息
     */
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(@Validated Customer customer) {
        if (CMSConstants.CUSTOMER_NAME_NOT_UNIQUE.equals(customerService.checkCustomerNameUnique(customer))) {
            return error("新增类别'" + customer.getCname() + "'失败，类别名称已存在");
        }
        return toAjax(customerService.insertCustomer(customer));
    }

    /**
     * 批量删除客户信息
     *
     * @param ids
     * @return
     */
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String ids) {
        try {
            return toAjax(customerService.deleteCustomerByIds(ids));
        } catch (Exception e) {
            return error(e.getMessage());
        }
    }

    /**
     * 修改客户信息
     */
    @GetMapping("/edit/{cid}")
    public String edit(@PathVariable("cid") Long cid, ModelMap mmap) {
        // 根据类别ID查询订单类别信息
        mmap.put("customer", customerService.selectCustomerById(cid));
        return prefix + "/edit";
    }

    /**
     * 修改保存客户信息
     */
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(@Validated Customer customer) {
        if (CMSConstants.CUSTOMER_NAME_NOT_UNIQUE.equals(customerService.checkCustomerNameUnique(customer))) {
            return error("修改类别'" + customer.getCname() + "'失败，类别名称已存在");
        }
        return toAjax(customerService.updateCustomer(customer));
    }

    /**
     * 查看客户信息详情
     *
     * @param cid
     * @param mmap
     * @return
     */
    @GetMapping("/detail/{cid}")
    public String detail(@PathVariable("cid") Long cid, ModelMap mmap) {
        mmap.put("name", "customer");
        mmap.put("customer", customerService.selectCustomerById(cid));
        return prefix + "/detail";
    }

    /**
     * 校验客户名称是否唯一
     */
    @PostMapping("/checkCustomerNameUnique")
    @ResponseBody
    public String checkCustomerNameUnique(Customer customer) {
        return customerService.checkCustomerNameUnique(customer);
    }
}
